package com.yizhuoyan.his.serviceimpl;

import com.yizhuoyan.his.ao.AddCaseCheckAo;
import com.yizhuoyan.his.ao.CaseCheckProjectAo;

import com.yizhuoyan.his.ao.UpdateCaseCheckAo;
import com.yizhuoyan.his.dao.CaseCheckRecordDao;
import com.yizhuoyan.his.entity.CaseCheckProjectEntity;
import com.yizhuoyan.his.entity.CaseCheckRecordEntity;
import com.yizhuoyan.his.service.doctor.medical.CaseCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Transactional
@Service
public class CaseCheckServiceImpl implements CaseCheckService {

    @Autowired
    CaseCheckRecordDao ccrd;

    @Override
    public List<CaseCheckProjectAo> queryCaseCheck(Long caseMedicalId) throws Exception {
        //验证参数
        if (caseMedicalId==null)
            return null;
        List<CaseCheckProjectEntity> caseCheckProjectEntities = ccrd.selectCase(caseMedicalId);
        List<CaseCheckProjectAo> ccpas=new LinkedList<CaseCheckProjectAo>();
        CaseCheckProjectAo caseCheckProjectAo = new CaseCheckProjectAo();
        for (CaseCheckProjectEntity s: caseCheckProjectEntities) {
            caseCheckProjectAo.setProjectName(s.getProjectName());
            caseCheckProjectAo.setPrice(s.getPrice());
            caseCheckProjectAo.setGender(s.getGender());
            caseCheckProjectAo.setCaseMedicalId(s.getMedicalRecordId());
            caseCheckProjectAo.setCaseName(s.getCaseName());
            caseCheckProjectAo.setCreateTime(s.getCreateTime());
            ccpas.add(caseCheckProjectAo);
        }
        return ccpas;
    }

    @Override
    public boolean modifyCaseCheckState(Long caseMedicalId) throws Exception{
        //验证参数
        if (caseMedicalId==null)
            return false;
        ccrd.updateState(caseMedicalId);
        return true;

    }

    @Override
    public boolean modifyCaseCheckResult(@Validated UpdateCaseCheckAo ao) throws Exception {
        long id = ccrd.selectByExpenseItemId(ao.getCaseMedicalId(), ao.getNondrugExpenseItemId());
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("result",ao.getResult());
        map.put("checkTime", LocalDate.now());
        map.put("resultOutTime",LocalDate.now());
        ccrd.update(id,map);
        return false;
    }

    @Override
    public boolean addCaseCheck(@Validated AddCaseCheckAo acao) throws Exception {
        long id = ccrd.selectRegistrationId(acao.getCaseMedicalId(), acao.getCaseRegistrationId());
        CaseCheckRecordEntity caseCheckRecordEntity = ccrd.selectById(id);
        caseCheckRecordEntity.setAmount(acao.getAmount());
        caseCheckRecordEntity.setRecordType(acao.getRecordType());
        caseCheckRecordEntity.setNodrugExpenseItemId(acao.getNondrugExpenseItemId());
        caseCheckRecordEntity.setCreateTime(LocalDateTime.now());
        return true;
    }
}
